package com.superteam.mac.web.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.superteam.mac.base.data.DataBean;
import com.superteam.mac.base.web.action.BaseAction;
import com.superteam.mac.mode.IndexConfig;
import com.superteam.mac.mode.MacRealTimeBean;
import com.superteam.mac.mode.MachineRec;
import com.superteam.mac.mode.RealTimeBean;
import com.superteam.mac.mode.WorkShop;
import com.superteam.mac.servlet.MacStautData;
import com.superteam.mac.servlet.ReceiveData;

public class RealTimeAction extends BaseAction {
	
	public ActionForward findWorkShopAndMachine(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			List<WorkShop> workshops = super.getWrokShopService().findWorkShopAndMachines();
			for(WorkShop work:workshops)
			{
				List<MachineRec> machines = work.getMachines();
				for(MachineRec machine:machines)
				{
					System.out.println(work.getWorkShopName()+"++88aaaa888++++++"+machine.getMachineName());
				}
			}
			request.setAttribute("shops", workshops);
		return mapping.findForward("showworkshop");
	}
	/**
	 * ajax target
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward changeMachineOrWorkshop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String workshop = request.getParameter("workshop");
		String machine = request.getParameter("machine");
		
		if(workshop != null)
		request.getSession().setAttribute("real_line_cur_workshop", workshop);
		
		if(machine != null)
		request.getSession().setAttribute("real_line_cur_machine", machine);
		return null;
	}
	
	
	
	public ActionForward findMachineIndex(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Map<String,List<RealTimeBean>>> ds = ReceiveData.datas;
		
		List<RealTimeBean> allbeans = new ArrayList<RealTimeBean>();
		for(Map<String,List<RealTimeBean>> map : ds)
		{
			for(java.util.Iterator<String> iter = map.keySet().iterator();iter.hasNext();)
			{
				String key = iter.next();
				allbeans.addAll(map.get(key));
			}
			
		}
		for(RealTimeBean b:allbeans)
		{
			IndexConfig config = DataBean.getIndexName(b.getIndexConfig());
			b.setIndexUnit(config.getIndexUnit());
		}
		//分拣数据 按 车间 机床
		String workshop = (String)request.getSession().getAttribute("real_line_cur_workshop");
		String machine = (String)request.getSession().getAttribute("real_line_cur_machine");
		
		List<RealTimeBean> beans = new ArrayList<RealTimeBean>();
		if(workshop == null && machine == null)
		{
			beans.addAll(allbeans);
		}
		
		if(workshop != null && machine == null)
		{
			for(RealTimeBean b:allbeans)
			{
				if(workshop.equals(b.getWorkShopName()))
				{
					beans.add(b);
				}
			}
		}
		if(workshop != null && machine != null)
		{
			for(RealTimeBean b:allbeans)
			{
				if(workshop.equals(b.getWorkShopName())&&machine.equals(b.getMachineName()))
				{
					beans.add(b);
				}
			}
		}
		
		
		request.setAttribute("beans",beans);
		return mapping.findForward("monitor_right2");
	}
	
	public ActionForward getMachineRealStaut(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String,MacRealTimeBean> machineStaut = MacStautData.machines;
		String workshop = (String)request.getSession().getAttribute("real_line_cur_workshop");
		String machine = (String)request.getSession().getAttribute("real_line_cur_machine");
		
		
		Collection<MacRealTimeBean> list = machineStaut.values();
		for(MacRealTimeBean b:list)
		{
			String machineName = DataBean.getMachineName(b.getMachineCode());
			System.out.println(b.getMachineCode()+" "+b.getMachineName()+" "+b.getWorkShopCode());
			b.setMachineName(machineName);
		}
		//0102 机床2 车间1
		List<MacRealTimeBean> outlist = new ArrayList<MacRealTimeBean>();
		if(workshop == null && machine == null)
		{
			outlist.addAll(list);
		}
		if(workshop != null && machine == null)
		{
			for(MacRealTimeBean b:list)
			{
				if(workshop.equals(b.getWorkShopCode()))
				{
					outlist.add(b);
				}
			}
		}
		
		if(workshop != null && machine != null)
		{
			for(MacRealTimeBean b:list)
			{
				if(workshop.equals(b.getWorkShopCode())&&machine.equals(b.getMachineName()))
				{
					outlist.add(b);
				}
			}
		}
		
		request.setAttribute("beans", outlist);
		return mapping.findForward("monitor_right1");
	}
	
	public ActionForward getMachineRealTimeLine(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String config = request.getParameter("indexconfig");
		Collection<IndexConfig> indexs  = DataBean.CONFIGS.values();
		if(null==config&&indexs.size()>0)
		{
			
			config = indexs.iterator().next().getIndexName();
			
		}
		String machieName = (String)request.getSession().getAttribute("real_line_cur_machine");
		if(machieName == null)
		{
			machieName = "";
		}
		
		String xml = "<chart bgColor='000000' bgAlpha='100' canvasBorderThickness='1' "+
						"canvasBorderColor='008040' canvasBgColor='000000' yAxisMaxValue='100' "+
						"decimals='0' numdivlines='9' numVDivLines='28' numDisplaySets='30' "+
						"divLineColor='008040' vDivLineColor='008040' divLineAlpha='100' "+
						"chartLeftMargin='10' baseFontColor='00dd00' showRealTimeValue='0' "+
						"dataStreamURL='data.jsp' refreshInterval='2' "+
						"numberSuffix='' labelDisplay='rotate' slantLabels='1' "+
						"toolTipBgColor='000000' toolTipBorderColor='008040' baseFontSize='11' "+
						"showAlternateHGridColor='0' legendBgColor='000000' "+
						"legendBorderColor='008040' legendPadding='35' showLabels='1'> "+
						"<categories> "+
							"<category label='Start' /> "+
						"</categories> "+
						"<dataset color='00dd00' seriesName='"+machieName+"    "+config+"' showValues='0' "+
							"alpha='100' anchorAlpha='0' lineThickness='2'> "+
							"<set value='0' /> "+
						"</dataset> "+
						//"<dataset color='ff5904' seriesName='Processor B' showValues='0' "+
						//	"alpha='100' anchorAlpha='0' lineThickness='2'> "+
							//"<set value='0' /> "+
						//"</dataset> "+
					"</chart>";
		
		request.getSession().setAttribute("indexconfig", config);
		request.getSession().setAttribute("real_line_cur_config", config);
		request.getSession().setAttribute("xml", xml);	
		
		request.getSession().setAttribute("indexs", indexs);
		response.sendRedirect("/macmanage/html/monitor/monitor_right3.jsp");
		return null;
		//return mapping.findForward("monitor_right3");
	}
}
